﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Hxsoft.AppV1;
using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1.Admin.UserManage
{
	public partial class SetUserRoles : BasePage
	{
		private string _identifier;
		private bool _isSetIdentifier = false;

		/// <summary>
		/// 当前修改信息的标识符。
		/// </summary>
		protected string Identifier
		{
			get
			{
				if (!_isSetIdentifier)
				{
					_identifier = Request.QueryString["UserName"];
					_isSetIdentifier = true;
				}

				return _identifier;
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if (RightValidate())
			{
				if (!IsPostBack)
				{
					RoleSelector.ExcludePropertyCode = Roles.RolePropertyCode.DisallowUserAccredit;
					RoleSelector.ExcludeRoleIds = Roles.GetRolesForUser(Identifier);

					LoadList();
					Page.DataBind();
				}
			}
		}

		protected void List_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
			{
				Role role = (Role)e.Item.DataItem;
				LinkButton LbtDelete = (LinkButton)e.Item.FindControl("LbtDelete");

				if (Roles.GetAccreditRight(role))
					LbtDelete.Enabled = true;
				else
					LbtDelete.Enabled = false;
			}
		}

		protected void List_ItemCommand(object source, RepeaterCommandEventArgs e)
		{
			Guid identifier = GetListItemIdentifier(e.Item);
			string messages = "";

			switch (e.CommandName)
			{
				case "Delete":
					messages = Delete(identifier);
					break;
			}

			if (!String.IsNullOrEmpty(messages))
			{
				Messages.Add(new PageMessage(true, null, PageMessageType.Error, Res.OperationErrorTitle, messages));
			}

			LoadList();
			RoleSelector.ExcludeRoleIds = Roles.GetRolesForUser(Identifier);
			RoleSelector.Reload();
		}

		protected void RoleSelector_SelectFinish(object sender, EventArgs e)
		{
			if (RightValidate() && IsValid)
			{
				foreach (Guid roleId in RoleSelector.SelectedItems)
				{
					Roles.AddUsersToRole(new string[] { Identifier }, roleId);
				}

				Messages.Add(new PageMessage(true, null, PageMessageType.Prompt, Res.OperationCompleteTitle, Res.OperationComplete_UserAccredit));

				LoadList();
				RoleSelector.ExcludeRoleIds = Roles.GetRolesForUser(Identifier);
				RoleSelector.Reload();
			}
		}

		protected void CtrShowRoleSelectorButton_Click(object sender, EventArgs e)
		{
			RoleSelector.Visible = !RoleSelector.Visible;
		}

		/// <summary>
		/// 加载列表。
		/// </summary>
		protected void LoadList()
		{
			List.DataSource = Roles.GetUserAccreditList(Identifier);
			List.DataBind();
		}

		/// <summary>
		/// 取得 ListItem 的标识符。
		/// </summary>
		/// <param name="item">列表的 ListItem。</param>
		/// <returns>ListItem 的标识符。</returns>
		protected Guid GetListItemIdentifier(RepeaterItem item)
		{
			Guid identifier;
			HiddenField control;

			control = (HiddenField)item.FindControl("Identifier_RoleId");
			identifier = new Guid(control.Value);

			return identifier;
		}

		/// <summary>
		/// 取得 ListItem 的站点名称。
		/// </summary>
		/// <param name="item">列表的 ListItem。</param>
		/// <returns> ListItem 的站点名称。</returns>
		protected string GetListItemSiteName(RepeaterItem item)
		{
			int? siteId = ((Role)item.DataItem).SiteId;
			if (siteId.HasValue)
				return Sites.GetSiteName(siteId.Value);
			return null;
		}

		/// <summary>
		/// 删除指定的信息
		/// </summary>
		/// <param name="roleName">要删除信息的标识符。</param>
		/// <returns>如果删除发生错误则返回错误消息。</returns>
		protected string Delete(Guid roleId)
		{
			if (Roles.GetAccreditRight(Roles.GetItem(roleId)))
			{
				Roles.RemoveUsersFromRole(new string[] { Identifier }, roleId);
				return null;
			}
			else
				return Res.PermissionException_DeleteAppointedObject.Replace("{0}", roleId.ToString());
		}

		/// <summary>
		/// 返回权限验证结果，如果验证失败则显示消息提示。
		/// </summary>
		/// <returns>布尔值代表权限验证是否通过。</returns>
		public bool RightValidate()
		{
			bool right = Users.GetSettingRight(Users.GetUser(Identifier));
			if (!right)
			{
				Messages.Add(new PageMessage(false, null, PageMessageType.Error, Res.PermissionExceptionMessageTitle, Res.PermissionExceptionMessage));
			}
			return right;
		}
	}
}
